<?php
/**
 * Created by PhpStorm.
 * User: zhaojunlike
 * Date: 2016/12/1
 * Time: 13:18
 */

namespace Home\Controller;


use Common\Helper\HttpHelper;
use Think\Controller;

class PublicController extends Controller
{

    public function verify()
    {
        $config = array('length' => 4);
        $Verify = new \Think\Verify($config);
        ob_clean();
        $Verify->entry();
    }

    public function logout()
    {
        session('user_login_id', null);
        $this->success("注销成功，欢迎您下次登陆", U('public/login'));
    }

    public function login()
    {
        //登录
        $log = M('user');
        $information = M('publish');
        $infor = $information->order('s_id desc')->limit(5)->select();
        $infor = $infor[0]['publish'];
        $this->assign('infor', $infor);
        if (IS_POST) {
            $obj = new \Think\Verify();
            if (!$obj->check(I('captcha'))) {
                // $this->error("验证码错误");
            }
            $user = I('post.user');
            $password = I('post.password');
            $result = M('user')->where(array('u_user' => $user))->find();
            if (!$result) {
                $this->error("用户不存在,请先注册");
            }
            //判断密码
            if ($result['u_password'] != encryptUserPwd($user, $password)) {
                $this->error("密码错误,请重新尝试");
            }
            //判断状态
            if ($result['status'] != 1) {
                $this->error('用户状态不可用');
            }
            if ($result) {
                session('user_login_id', $result['u_id']);
                $this->success('登录成功', U('User/center'));
            } else {
                $this->error('密码错误', 'index');
            }
        }
        $this->display();
    }

    public function find_pwd()
    {
        $param = $_SESSION['param'];
        $checkcode = I('post.checkcode', '', 'htmlspecialchars');
        if (IS_POST) {
            if ($checkcode == $param) {
                $user = M('user');
                $data['u_user'] = I('post.phone', '', 'htmlspecialchars');
                $u_user = $data['u_user'];
                $data['u_password'] = I('post.password', '', 'htmlspecialchars');
                $rel = $user->where("u_user='$u_user'")->save($data);
                if ($rel) {
                    $this->success('修改成功', 'index', true);
                } else {
                    $this->error('无此用户', 'changepwd', true);
                }
            } else {
                $this->error('验证码错误', 'changepwd', true);
            }
        }

        $this->display();
    }

    //短信接口
    public function getPhoneCode($phone)
    {
        $checkRet = checkPhoneExist($phone);
        if ($checkRet) {
            $this->error('对不起,用户已经存在了');
        }
        $url = "http://api.id98.cn/api/sms";
        $param = rand(100000, 999999);
        //验证是否正确的手机号码
        $post_data = array(
            "appkey" => "8e3067c0140e3d3cc6ad64abd4becfeb",
            "phone" => "$phone",
            "templateid" => "1000",
            "param" => "$param"
        );

        $ret = HttpHelper::http($url, $post_data, 'POST');
        $retObj = json_decode($ret, true);
        if ($retObj['errcode'] == 0) {
            //发送成功了，才会写入验证码
            session('user_phone_code', $param);
            $this->success("发送成功");
        } else {
            $this->error("发送失败");
        }
    }

    //注册
    public function register_email()
    {
        $log = M('user');
        if (IS_POST) {
            $data['u_user'] = I('post.user');
            $u_user = $data['u_user'];
            $data['u_password'] = I('post.password');
            $result = $log->query("select * from user where u_user='$u_user'");
            if (!$result) {
                if ($data['u_password'] == $data['u_password2']) {
                    $log->create();
                    $data['u_password'] = md5($data['u_password']);
                    if ($log->field('u_user,u_password')->add($data)) {
                        session_start();
                        $_SESSION['user'] = $data['u_user'];
                        $this->success('index', '注册成功', 3);
                    } else {
                        $this->error('注册失败');
                    }
                } else {
                    $er = "密码不一致";
                    $this->assign('er', $er);
                }
            } else {
                $re = "账号已被占用";
                $this->assign('re', $re);
            }
        }
        $this->display();
    }

    //手机注册 OK
    public function register_phone()
    {
        if (IS_POST) {
            //验证码
            $phoneCode = session('user_phone_code');
            if (!$phoneCode) {
                $this->error("请先获取验证码后再登陆");
            }
            $checkCode = I('post.check_code');
            if ($checkCode == $phoneCode) {
                $data['u_password'] = I('post.password');
                $data['u_password2'] = I('post.password2');
                if (!checkPhoneExist(I('user'))) {
                    if ($data['u_password'] == $data['u_password2']) {
                        $user = M('user')->create();
                        $user['u_user'] = I('user');
                        $user['create_time'] = time();
                        $user['u_type'] = I('u_type');
                        $user['u_phone'] = I('user');
                        $user['u_name'] = "用户" . I('user');
                        $user['u_password'] = encryptUserPwd(I('user'), $data['u_password']);
                        if (M('user')->add($user)) {
                            //销毁验证码
                            session('user_phone_code', null);
                            $this->success('注册成功,请先登陆系统', U('public/login'));
                        } else {
                            $this->error('注册失败');
                        }
                    } else {
                        $this->error("两次密码不一致");
                    }
                } else {
                    $this->error("手机号已被注册");
                }
            } else {
                $this->error('验证码错误');
            }

        }
    }
}